package client.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import client.ClientController;
import dto.MemberDTO;
import dto.SongDTO;
import dto.SongInfoDTO;

public class PlaySong extends JFrame {
	
	private JTextField user;
	private JPasswordField password;
	private JTextField title;
	private JTextField artist;
	private JTextField duration; 
	private JTextField releaseDate;
	private JTextField pricePerPlay;
	private JButton play;
	private JTextArea lyrics;
	private JButton atras;
	
	private ClientController clientController;
	
	public PlaySong (ClientController clientController, String userName, String pass, SongInfoDTO song) throws RemoteException{
		init ();
		this.clientController = clientController;
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setResizable(false);
		user.setText(userName);
		password.setText(pass);
		this.setLocationRelativeTo(null);
		loadSong(this.clientController.play(song, new MemberDTO(user.getText())));
	}
	private void init (){
		this.setTitle("Tralala");
		user = new JTextField();
		password = new JPasswordField();
		title = new JTextField(20);
		artist = new JTextField(19);
		duration = new JTextField(13);
		duration.setHorizontalAlignment(JTextField.RIGHT);
		releaseDate = new JTextField(15);
		pricePerPlay = new JTextField(12);
		pricePerPlay.setHorizontalAlignment(JTextField.RIGHT);
		play = new JButton();
		play.setText("Play");
		play.setEnabled(false);
		atras = new JButton();
		atras.setText("Back");
		atras.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					SwingUtilities.invokeLater(new Runnable() {
						
						@Override
						public void run() {
							new SearchSong(clientController,user.getText(),password.getPassword().toString());
							
						}
					});
				} catch(Exception exp) {
					exp.printStackTrace();
				}
				dispose ();
			}
		});
		lyrics = new JTextArea();
		lyrics.setColumns(23);
		lyrics.setRows(15);
		JPanel mainPanel = new JPanel();
		BoxLayout mainBox = new BoxLayout(mainPanel, BoxLayout.X_AXIS);
		mainPanel.setLayout(mainBox);
		mainPanel.add(panelLeft());
		mainPanel.add(panelRight());
		this.add(mainPanel);
	}
	
	private JPanel panelLeft (){
		JPanel panelLeft = new JPanel();
		panelLeft.setLayout(null);
		
		JPanel panelUser = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelUser.setBounds(0, 0, 300, 90);
		panelUser.setBorder(BorderFactory.createTitledBorder("Users"));
		panelUser.add(user);
		panelLeft.add(panelUser);
		
		JPanel panelPassword = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelPassword.setBounds(0, 95, 300, 90);
		panelPassword.setBorder(BorderFactory.createTitledBorder("Password"));
		panelPassword.add(password);
		panelLeft.add(panelPassword);		
		
		JPanel panelSongInfo = new JPanel();
		BoxLayout box = new BoxLayout(panelSongInfo, BoxLayout.Y_AXIS);
		panelSongInfo.setLayout(box);
		panelSongInfo.setBounds(0, 185, 300, 270);
		
		panelSongInfo.setBorder(BorderFactory.createTitledBorder("Song information"));
		JPanel temp1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		temp1.add(new JLabel("Title: "));
		temp1.add(title);
		panelSongInfo.add(temp1);
		JPanel temp2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		temp2.add(new JLabel("Artist: "));
		temp2.add(artist);
		panelSongInfo.add(temp2);
		JPanel temp3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		temp3.add(new JLabel("Price per play: "));
		temp3.add(pricePerPlay);
		temp3.add(new JLabel("EUR"));
		panelSongInfo.add(temp3);
		JPanel temp4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		temp4.add(new JLabel("Duration: "));
		temp4.add(duration);
		temp4.add(new JLabel("seconds"));
		panelSongInfo.add(temp4);
		JPanel temp5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		temp5.add(new JLabel("Release Date: "));
		temp5.add(releaseDate);
		panelSongInfo.add(temp5);
		
		panelLeft.add(panelSongInfo);
		
		atras.setBounds(0, 460, 100, 50);
		
		panelLeft.add(atras);
		
		return panelLeft;
		
	}
	
	private JPanel panelRight(){
		JPanel pr = new JPanel();
		pr.setLayout(null);
		
		JPanel panelLyrics = new JPanel();
		panelLyrics.setBounds(0, 0, 300, 300);
		panelLyrics.setBorder(BorderFactory.createTitledBorder("Lyrics"));
		JScrollPane sp = new JScrollPane(lyrics);
		panelLyrics.add(sp);
		pr.add(panelLyrics);
		
		JPanel panelButtons = new JPanel();
		panelButtons.setBounds(105, 410, 100, 100);
		panelButtons.setBorder(BorderFactory.createEmptyBorder(-5, -5, -5, -5));
		panelButtons.add(play);
		pr.add(panelButtons);
		return pr;
	}
	
	private void loadSong (SongDTO song){
		title.setText(song.getTitle());
		artist.setText(song.getArtist());
		pricePerPlay.setText(Double.toString(song.getPricePerPlay()));
		duration.setText(Integer.toString(song.getDuration()));
		releaseDate.setText(new Date(song.getReleaseDate()).toString());
		lyrics.setText(song.getLyrics());
		JOptionPane.showMessageDialog(PlaySong.this, "This song is playing:\n"+song.getSong(), song.getTitle(), JOptionPane.INFORMATION_MESSAGE);
	}
	
	private String getPasswordString (){
		String r = "";
		for (char a : password.getPassword()){
			r += a;
		}
		return r;
	}
	
	/**
	 * @param args
	 * @throws RemoteException 
	 */
	public static void main(String[] args) throws RemoteException {
		
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
			new PlaySong(null, "Joseba", "joseba", null);
		} catch (Exception e) {
		}
	
	}

}
